package cn.gson.crmdemo.model.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.ComplaintDao;
import cn.gson.crmdemo.model.dao.CustomerDao;
import cn.gson.crmdemo.model.mapper.ComplaintMapper;
import cn.gson.crmdemo.model.pojo.Complaint;
import cn.gson.crmdemo.model.pojo.Customer;
import cn.gson.crmdemo.model.pojo.Quotation;
import cn.gson.crmdemo.model.pojo.Salesopportunity;
import cn.gson.crmdemo.model.vo.ComplaintCustomerVo;
import cn.gson.crmdemo.model.vo.QuoSaleCusProVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class ComplaintService {
	@Autowired
	CustomerDao cusdao;
	@Autowired
	ComplaintDao cdao;
	@Autowired
	ComplaintMapper cm;
	/**
	 * 查所有
	 * @return
	 */
	public JSONArray selectComplaint(){
		return cm.selectComplaint();
	}
	
	/**
	 * 新增
	 * @param dept 由用户提交到Controller
	 */
	public void addCom(ComplaintCustomerVo complaint) {
		Complaint com = new Complaint();
		com.setComplaintDate(complaint.getComplaintDate());
		com.setComplaintDescribe(complaint.getComplaintDescribe());
		com.setComplaintEmpName(complaint.getComplaintEmpName());
		com.setComplaintRemark(complaint.getComplaintRemark());
		com.setComplaintTheme(complaint.getComplaintTheme());
		com.setCustomerFeedback(complaint.getCustomerFeedback());
		com.setProcessingResult(complaint.getProcessingResult());
		com.setEmergencyLevel(complaint.getEmergencyLevel());
		
		Customer cusId = cusdao.findById(complaint.getCustomer()).get();
		com.setCustomer(cusId);
		cdao.save(com);
	}
	
	/**
	 * 修改
	 * @param dept 由用户提交到Controller
	 */
	public void updateCom(ComplaintCustomerVo complaint) {
		Complaint com = new Complaint();
		com.setComplaintId(complaint.getComplaintId());
		com.setComplaintDate(complaint.getComplaintDate());
		com.setComplaintDescribe(complaint.getComplaintDescribe());
		com.setComplaintEmpName(complaint.getComplaintEmpName());
		com.setComplaintRemark(complaint.getComplaintRemark());
		com.setComplaintTheme(complaint.getComplaintTheme());
		com.setCustomerFeedback(complaint.getCustomerFeedback());
		com.setProcessingResult(complaint.getProcessingResult());
		com.setEmergencyLevel(complaint.getEmergencyLevel());
		
		Customer cusId = cusdao.findById(complaint.getCustomer()).get();
		com.setCustomer(cusId);
		cdao.save(com);
	}
	
	/**
	 * 根据id查
	 * @param quotationId
	 * @return
	 */
	public JSONArray selectComplaintId(Integer complaintId) {
		return cm.selectComplaintId(complaintId);
	}
	
	public JSONArray selectValue(int value ) {
		System.err.println("value111111111111111"+value);
		if(value==1||value==2||value==3) {
		
			return cm.selectEmergencyLevel(value);
		}else {
			
			return cm.selectProcessingResult(value);
		}
	}
	
	public JSONArray selectMoHuComplaintName(String complaintTheme){
		return cm.selectMoHuComplaintName(complaintTheme);
	}

	
	public int updateStatus2(Integer complaintId) {
		return cm.updateStatus2(complaintId);
		
	}
}
